﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CST_3DPoints.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		Dialog for 3D points settings
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports System.Windows.Forms
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '




'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '

Public Class CST_3DPoints

    '// --------------------------------------------------------------------- '
    '// DATA

    '// ===================================================================== '




    ' /////////////////////////////////////////////////////////////////////////
#Region "Private functions"

#End Region
    ' /////////////////////////////////////////////////////////////////////////





    ' /////////////////////////////////////////////////////////////////////////
#Region "Public functions"

#End Region
    ' /////////////////////////////////////////////////////////////////////////





    ' /////////////////////////////////////////////////////////////////////////
#Region "Event handlers"


    '// --------------------------------------------------------------------- '
    Private Sub OK_Button_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) _
                                Handles OK_Button.Click

        Try

            Dim lcSTG As New PT3DSTG

            ' create a local copy of global settings
            lcSTG = CM_3DPoints.Stgs


            '// Separators
            If (LS_Sep.Items.Count = 0)
                MsgBox(CMain._T(TX.PT3D_EFLD))
                Return
            Else
                ReDim lcSTG.FSep(0 to LS_Sep.Items.Count - 1)
                For i = 0 to LS_Sep.Items.Count - 1
                    lcSTG.FSep(i) = _
                        CM_3DPoints.GetSepFromStr(LS_Sep.Items(i).ToString())
                Next
            End If
            lcSTG.DecSep = Me.TX_DecSep.Text '// decimal separator

            '// Layers
            lcSTG.lyCode = cbLyCode.Text
            lcSTG.lyMark = cbLyMark.Text
            lcSTG.lyNr = cbLyNr.Text
            lcSTG.lyZ = cbLyZ.Text
            

            '// Format
            For i = 0 to LS_Order.Items.Count - 1
                lcSTG.FOrd(i) = CM_3DPoints.GetOrdFormStr(LS_Order.Items(i).ToString())
            Next i
            lcSTG.bIgnErr = CK_IgnErr.Checked

            If (String.IsNullOrEmpty(Me.tx_CoordForm.Text) = False) Then
                CM_3DPoints.Stgs.sFormCoord = Me.tx_CoordForm.Text
            Else
                CM_3DPoints.Stgs.sFormCoord = Nothing
            End If

            '// Blocks
            lcSTG.doBlock = RD_CBlock.Checked   
            lcSTG.sBlockName = CB_BlkName.Text


            '// Text
            lcSTG.sTxSty = CB_TxSty.Text
            lcSTG.txH = CDbl(TX_TXH.Text)

            '// Check a bit what was entered
            If (lcSTG.lyMark = "") Then
                lcSTG.lyMark = "0"
            End If
            If (lcSTG.lyNr = "") Then
                lcSTG.lyNr = "0"
            End If
            If (lcSTG.lyZ = "") Then
                lcSTG.lyZ = "0"
            End If
            If (lcSTG.lyCode = "") Then
                lcSTG.lyCode = "0"
            End If
            If (lcSTG.sBlockName = "") Then
                lcSTG.sBlockName = "PCT3D"
            End If


            CM_3DPoints.Stgs = lcSTG
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
            
        Catch ex As Exception
		    Dim trace As System.Diagnostics.StackTrace = _
                New System.Diagnostics.StackTrace(ex, True)
		    Dim exceptionLineNumber As Integer =  _
                trace.GetFrame(0).GetFileLineNumber()
		    MsgBox(CMain._T(TX.EXC_IN) & trace.GetFrame(0).GetMethod.Name & vbCrLf & _
		     CStr(ex.Message) & "; " & vbCrLf & _
		     trace.GetFrame(0).ToString & vbCrLf)
        End Try
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) _
                                     Handles Cancel_Button.Click

        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CST_3DPoints_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load

        Dim i As Integer


        '----------- [ Static text ] --------------------
        Me.Text = CMain._T(TX.FSK_PT3D) & ". " & _
                CMain._T(TX.SETTINGS) & "."
        Me.OK_Button.Text = CMain._T(TX.OK)
        Me.Cancel_Button.Text = CMain._T(TX.CANCEL)

        '// Labels for tab
        Me.TabP_Block.Text = CMain._T(TX.BLOCK)
        Me.TabP_FForm.Text = CMain._T(TX.FORMAT)
        Me.TabP_Lay.Text = CMain._T(TX.LAYS)
        Me.TabP_Sep.Text = CMain._T(TX.SEPS)
        Me.TabP_Text.Text = CMain._T(TX.TEXT)


        '// Separators
        Me.B_SepAdd.Text = CMain._T(TX.ADD)
        Me.B_SepRem.Text = CMain._T(TX.REMOVE)
        Me.L_DecSep.Text = CMain._T(TX.DECSEP)

        '// Layers
        Me.L_Mark.Text = CMain._T(TX.MARKER)
        Me.L_NrPt.Text = CMain._T(TX.PTNR)
        Me.L_Descr.Text = CMain._T(TX.DESCR)
        Me.L_Height.Text = CMain._T(TX.HEIGHT)

        '// Format
        Me.CK_IgnErr.Text = CMain._T(TX.IGNERR)
        Me.lb_CoordForm.Text = CMain._T(TX.COORDFORM)

        '// Block
        Me.RD_CBlock.Text = CMain._T(TX.CREATEBLK)
        Me.RD_CEnt.Text = CMain._T(TX.CREATEENT)
        Me.L_BlkName.Text = CMain._T(TX.BLKNAME)

        '// Text
        Me.L_Style.Text = CMain._T(TX.STYLE)
        Me.lb_CoordForm.Text = CMain._T(TX.HTEXT)


        '----------- [ Actual settings ] --------------------
        Dim acDoc As Document = _
        Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        If (acDoc is Nothing) tHEN
            Me.Close()
            Return
        End If
        Dim acCurDb As Database = acDoc.Database
        Using acTrans As Transaction = _
            acCurDb.TransactionManager.StartTransaction()
        Try
            
            '// returns the layer table for the current database
            Dim acLyrTbl As LayerTable = _
            DirectCast(acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead), _
                LayerTable)
            '// returns the block table for the current database
            Dim acBTab As BlockTable = _
            DirectCast(acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead), _
                BlockTable)
            '// returns the text style table for the current database
            Dim acTxSTab As TextStyleTable = _
            DirectCast(acTrans.GetObject(acCurDb.TextStyleTableId, OpenMode.ForRead), _
                TextStyleTable)


            '// Separators
            If (Not CM_3DPoints.Stgs.FSep Is Nothing) AndAlso _
                UBound(CM_3DPoints.Stgs.FSep) <> -1 Then

                For i = 0 To UBound(CM_3DPoints.Stgs.FSep)
                    LS_Sep.Items.Add( _
                        CM_3DPoints.GetSepStr( _
                            CM_3DPoints.Stgs.FSep(i)))
                Next
            End If
            Me.TX_DecSep.Text = CM_3DPoints.Stgs.DecSep

            '// Layers
            Me.cbLyCode.Text = CM_3DPoints.Stgs.lyCode
            Me.cbLyMark.Text = CM_3DPoints.Stgs.lyMark
            Me.cbLyNr.Text = CM_3DPoints.Stgs.lyNr
            Me.cbLyZ.Text = CM_3DPoints.Stgs.lyZ

            '' Step through the Layer table and get each layer name
            For Each acObjId As ObjectId In acLyrTbl
                Dim acLyrTblRec As LayerTableRecord
                acLyrTblRec = _
                    DirectCast(acTrans.GetObject(acObjId, OpenMode.ForRead), _
                    LayerTableRecord)

                Me.cbLyMark.Items.Add(acLyrTblRec.Name)
                Me.cbLyNr.Items.Add(acLyrTblRec.Name)
                Me.cbLyZ.Items.Add(acLyrTblRec.Name)
                Me.cbLyCode.Items.Add(acLyrTblRec.Name)
            Next

            '// Format
            For i = 0 To PT3PART.COUNT - 1
                Me.LS_Order.Items.Add( _
                    CM_3DPoints.GetStrFormOrd( _
                        CM_3DPoints.Stgs.FOrd(i) _
                                             ) _
                                  )
            Next i
            Me.CK_IgnErr.Checked = CM_3DPoints.Stgs.bIgnErr
            If (String.IsNullOrEmpty(CM_3DPoints.Stgs.sFormCoord) = False) Then
                Me.tx_CoordForm.Text = CM_3DPoints.Stgs.sFormCoord
            Else
                Me.tx_CoordForm.Text = ""
            End If

            '// Block
            Me.RD_CBlock.Checked = CM_3DPoints.Stgs.doBlock
            Me.RD_CEnt.Checked = Not CM_3DPoints.Stgs.doBlock
            Me.CB_BlkName.Text = CM_3DPoints.Stgs.sBlockName

                '// Step through the block table and print each name
			For Each acObjId As ObjectId In acBTab
				Dim acBEntry As BlockTableRecord = _
				DirectCast(acTrans.GetObject(acObjId, OpenMode.ForRead), _
				    BlockTableRecord)

                If (acBEntry.IsAnonymous = False AndAlso _
                    acBEntry.IsLayout = False) Then
					CB_BlkName.Items.Add(acBEntry.Name)
				End If
			Next

            '// Text
			For Each acObjId As ObjectId In acTxSTab
                Dim acTxSty As TextStyleTableRecord = _
				DirectCast(acTrans.GetObject(acObjId, OpenMode.ForRead), _
				    TextStyleTableRecord)
                Me.CB_TxSty.Items.Add(acTxSty.Name)
			Next
            If CM_3DPoints.Stgs.sTxSty <> Nothing andalso _
                CM_3DPoints.Stgs.sTxSty <> "" Then

                If acTxSTab.Has(CM_3DPoints.Stgs.sTxSty) Then
                    Me.CB_TxSty.Text = CM_3DPoints.Stgs.sTxSty
                Else
                    Dim acTxSty As TextStyleTableRecord = _
				    DirectCast(acTrans.GetObject(acTxSTab("Standard"), OpenMode.ForRead), _
				        TextStyleTableRecord)
                    acTxSTab.UpgradeOpen()
                    
                    Dim acNTxS As TextStyleTableRecord = _
                        DirectCast(acTxSty.Clone(), TextStyleTableRecord)
                    acNTxS.Name = CM_3DPoints.Stgs.sTxSty
                    acTxSTab.Add(acNTxS)
                    acTrans.AddNewlyCreatedDBObject(acNTxS,True)
                    Me.CB_TxSty.Text = CM_3DPoints.Stgs.sTxSty
                End If

            End If
            Me.TX_TXH.Text = CM_3DPoints.Stgs.txH.ToString("0.000")

            acTrans.Commit()
        Catch ex As Exception
            acTrans.Abort()
            CMain.DebugException(ex)
            Me.Close()
        End Try
        End Using '' Dispose of the transaction

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub TX_DecSep_TextChanged( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles TX_DecSep.TextChanged
            
        L_SampleDecS.Text = "=> 1" & TX_DecSep.Text & "2"
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_SepAdd_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_SepAdd.Click
        
        Dim sTmp As String, i As Integer
        sTmp = Me.TX_NewSep.Text
        If (sTmp.Length = 0)
            MsgBox(CMain._T(TX.ERR_EMTX))
            Return
        End If
        If (sTmp = " ") Then
            sTmp = "<" & CMain._T(TX.SPACE) & ">"
        ElseIf (sTmp = Chr(9)) Then
            sTmp = "<TAB>"
        End If
        For i = 0 to Me.LS_Sep.Items.Count - 1
            If (sTmp = Me.LS_Sep.Items(i).ToString) Then
                MsgBox(CMain._T(TX.PT3D_EDUPL))
                Return
            End If
        Next i
        Me.LS_Sep.Items.Add(sTmp)
        Me.TX_NewSep.Text = ""
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_SepRem_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_SepRem.Click
        
        If Me.LS_Sep.SelectedIndex <> -1
            Dim i As Integer = Me.LS_Sep.SelectedIndex
            Me.LS_Sep.Items.RemoveAt(Me.LS_Sep.SelectedIndex)
            If (Me.LS_Sep.Items.Count > 0) Then
                If (i >= Me.LS_Sep.Items.Count) Then
                    Me.LS_Sep.SelectedIndex = Me.LS_Sep.Items.Count-1
                Else
                    Me.LS_Sep.SelectedIndex = i
                End If
            End if
        End If

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub TX_TXH_Validating( _
        ByVal sender As System.Object,  _
        ByVal e As System.ComponentModel.CancelEventArgs) _
        Handles TX_TXH.Validating
        
        Dim d As Double
        Try
            d = CDbl(TX_TXH.Text)
        Catch ex As System.Exception
             MsgBox(CMain._T(TX.ENOTNUM))
             e.Cancel = True
             TX_TXH.Select(0, TX_TXH.Text.Length)
        End Try
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_LOrd_Up_Click( ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
                                 Handles B_LOrd_Up.Click
        
        Dim mSelectedIndex as integer
        Dim mOtherIndex as integer
        'do nothing if the top item is selected.
        If (Me.LS_Order.SelectedIndex = -1) Then Exit Sub
        If Me.LS_Order.SelectedIndex <> 0 Then
            mSelectedIndex = Me.LS_Order.SelectedIndex
            mOtherIndex = mSelectedIndex - 1
            LS_Order.Items.Insert(mSelectedIndex + 1, _
                                  LS_Order.Items(mOtherIndex))
            LS_Order.Items.RemoveAt(mOtherIndex)
            Me.LS_Order.SelectedIndex = mSelectedIndex-1
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_LOrd_Down_Click( ByVal sender As System.Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles B_LOrd_Down.Click
        
        Dim mSelectedIndex as integer
        Dim mOtherIndex as integer
        'do nothing if the bottom item is selected.
        If (Me.LS_Order.SelectedIndex = -1) Then Exit Sub
        If Me.LS_Order.SelectedIndex <> Me.LS_Order.Items.Count - 1 Then
            mSelectedIndex = Me.LS_Order.SelectedIndex
            mOtherIndex = mSelectedIndex + 1
            LS_Order.Items.Insert(mSelectedIndex, LS_Order.Items(mOtherIndex))
            LS_Order.Items.RemoveAt(mOtherIndex + 1)
            Me.LS_Order.SelectedIndex = mSelectedIndex+1
        End If
    End Sub
    '// ===================================================================== '

#End Region
    ' /////////////////////////////////////////////////////////////////////////


End Class

'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
